import sys; sys.path.insert(0,'third_party'); sys.path.insert(0,'.')
from twisted.internet import reactor, threads
from twisted.python.util import println
from common import crypto,RPCClient


if __name__ == '__main__':        
    rc = RPCClient('localhost', 8080, 3)      
    def sendHello(data = None):
        print "sendHello called in ", threading.currentThread().name
        print "data = ",data        
        if not rc.status:
            print "rc is disconnected"
        else:
            print "calling echo"        
            d = rc.echo('Hello')
            #d.addCallback(lambda x:println('server said %s' % str(x)))
            d.addCallback(sendHello)
            time.sleep(3)
    def requestDeferredService():
        print "requestLongService"
        if not rc.status:
            print "rc is disconnected"
        else:
            d = rc.largeFibonacciNumber()
            d.addCallback(handleDeferred)
    def handleDeferred(remoteDeferred):
        print "handleResult"
        remoteDeferred.addCallback(printResult)
    def printResult(x):
        print "the large fibonacci num is %d" % x
    #reactor.callLater(3, sendHello)                           
    #d = rc.get_public_key()
    #d.addCallback(rc.enable_encryption)
    #d.addCallback(sendHello)
    reactor.callLater(1, requestDeferredService)    
    reactor.run()